<template>
  <div class="notificationDetails warp">
    <div class="head">
      <!-- <router-link to="/message"> -->
      <div class="mar-right-10">消息中心</div>
      <!-- </router-link> -->
      <div class="arrow"></div>
      <div class="mar-right-10">公告详情</div>
    </div>
    <div class="messageDetail" v-loading="loading">
      <h1>{{ detail.noticeTitle }}</h1>
      <div class="date">{{ detail.createTime }}</div>
      <el-divider />
      <div class="messageInfo" :innerHTML="detail.noticeContent"></div>
    </div>
  </div>
</template>

<script setup>
import { defineComponent, ref, shallowRef, watch } from 'vue'
import { useRoute } from 'vue-router'
import { useUser } from '@/store/user'
import Cookie from 'js-cookie'
import {
  getUserInfo
} from '@/api/user/user.js'
import {
  getNoticeDetail
} from '@/api/user/notice.js'

defineComponent({
  name: 'NotificationDetails',
})
const userStore = useUser()
const route = useRoute();
const detail = ref({
  createTime: '',
  image: '',
  noticeContent: '',
  noticeId : '',
  noticeTitle : ''
});
const loading = shallowRef(false);

function getDetail () {
  loading.value = true
  getNoticeDetail({
    noticeId: route.query.id
  }).then(response => {
    const res = response.data
    if (res.code === '200') {
      detail.value = res.data
      if (Cookie.get('token')) {
        getUserInfoData()
      }
    }
  }).catch(err => {
    ElMessage.error(err)
  }).finally(() => {
    loading.value = false
  })
}

function getUserInfoData () {
  getUserInfo().then(response => {
    const res = response.data
    if (res.code === '200') {
      userStore.setUserInfo(res.data)
    }
  }).catch(err => {
    console.log(err)
  })
}

watch(
  () => route.query,
  (nVal) => {
    if (nVal.id) {
      getDetail()
    }
  },
  {
    immediate: true,
  }
)
</script>

<style lang="scss" scoped>
.notificationDetails {
  .head {
    width: 100%;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    display: flex;
    align-items: center;
    .arrow {
      background-image: url('../../../../static/image/xiangyou@2x.png');
      width: 5px;
      height: 10px;
      margin-right: 10px;
    }
  }
  .messageDetail {
    background: #FFFFFF;
    padding: 50px 0;
    min-height: 500px;
    h1 {
      font-size: 40px;
      font-weight: bold;
      color: #333333;
      text-align: center;
      margin-bottom: 20px;
    }
    .date {
      font-size: 14px;
      font-weight: 400;
      color: #666666;
      text-align: center;
      margin-bottom: 25px;
    }
    .messageInfo {
      min-height: 500px;
      p {
        font-size: 14px;
        color: #666666;
      }
    }
    .messageInfo :deep(img){ // 修改v-html渲染的样式
      max-width: 100%;
      height: auto;
    }
  }
}
</style>
